﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Seguridad;
using Dominio;

namespace HotelGUI
{
    public partial class UsuarioRoles : Form
    {
        private Security sec = Security.Instancia;
        private ISistema sistema =  Dominio.Fabrica.getSistema();

        public UsuarioRoles()
        {
            InitializeComponent();
            try
            {
                List<String[]> lst2 = new List<string[]>();   
                List<String[]> usuarios = sistema.obtenerUsuarios();
                foreach (string[] s in usuarios)
                {
                    cmbUsuarios.Items.Add(s[0] + "-" + s[1]);
                }
                lst2 = sec.traerRoles();
                foreach (string[] s in lst2)
                {
                    int n = dvgNoAsig.Rows.Add();
                    dvgNoAsig.Rows[n].Cells["IdNoAsig"].Value = s[0];
                    dvgNoAsig.Rows[n].Cells["descNoAsig"].Value = s[1];
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void cargaTablas(int idusu)
        {
            dvgAsignados.Rows.Clear();
            dvgNoAsig.Rows.Clear();
            List<String[]> lst = new List<string[]>();
            List<String[]> lst2 = new List<string[]>();
            try
            {
                lst = sec.traeRolesUsuario(idusu);
                foreach (string[] s in lst)
                {
                    int n = dvgAsignados.Rows.Add();
                    dvgAsignados.Rows[n].Cells["IdAsig"].Value = s[0];
                    dvgAsignados.Rows[n].Cells["descAsig"].Value = s[1];
                }
                lst2 = sec.traeNoRolesUsuario(idusu);
                foreach (string[] s in lst2)
                {
                    int n = dvgNoAsig.Rows.Add();
                    dvgNoAsig.Rows[n].Cells["IdNoAsig"].Value = s[0];
                    dvgNoAsig.Rows[n].Cells["descNoAsig"].Value = s[1];
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dvgAsignados_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }
            try
            {
                string[] cad = cmbUsuarios.SelectedItem.ToString().Split('-');
                int idusu = int.Parse(cad[0]);

                string rol = dvgAsignados.Rows[e.RowIndex].Cells["IdAsig"].Value.ToString();
                sec.bajaUsuarioRol(idusu, int.Parse(rol));
                cargaTablas(idusu);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dvgNoAsig_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }
            try
            {

                string[] cad = cmbUsuarios.SelectedItem.ToString().Split('-');
                int idusu = int.Parse(cad[0]);   
                string rol = dvgNoAsig.Rows[e.RowIndex].Cells["IdNoAsig"].Value.ToString();
                sec.altaUsuarioRol(idusu, int.Parse(rol));
                cargaTablas(idusu);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cmbUsuarios_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (cmbUsuarios.SelectedItem != null)
            {
                string[] cad = cmbUsuarios.SelectedItem.ToString().Split('-');
                int idusu = int.Parse(cad[0]);
                cargaTablas(idusu);
            }
        }
    }
}
